package darknet.core.impl.net;

import darknet.core.spec.net.MessageHeader;
import darknet.core.spec.net.NetworkManager;
import darknet.core.spec.net.Node;

/**
 * Base message factory.
 *
 * @author Yoyo
 */
public class BaseMessageFactory {

    /** Header for each chat message */
    public static final MessageHeader header;

    /** Class instance */
    private static BaseMessageFactory INSTANCE;

    /**
     * Static initialisation block.
     */
    static {
	header = new MessageHeaderImpl(BaseProtocol.getCurrent());
    }

    /**
     * Get an instance of the class.
     *
     * @param network The network manager to use
     * @return
     */
    public synchronized static BaseMessageFactory getInstance(
	    NetworkManager network) {

	if(INSTANCE == null) {
	    INSTANCE = new BaseMessageFactory(network);
	}

	return INSTANCE;
    }

    /** The network manager */
    private NetworkManager network;

    /**
     * Private constructor.
     */
    private BaseMessageFactory(NetworkManager network) {

	if (network == null) {
	    throw new IllegalArgumentException("network is null");
	}
	this.network = network;
    }

    /**
     * Make a new ping message.
     *
     * @param to      The receiver.
     * @return
     */
    public PingMessage makePingMessage(Node to) {
	return new PingMessage(header, network.myNode(), to);
    }

}
